package com.example.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipDown {
	public static void main(String[] args) {
//		File zipfile=new File("D:\\download\\logs\\test");
//		String path=copyFile("D:\\download\\logs\\test","D:\\download\\logs\\test\\1",".txt");
//		System.out.println(path);
		
		File f=new File("D:\\download\\logs\\test\\aaa");
		int length = (int) f.length();
        byte[] data = new byte[length];
        try {
			new FileInputStream(f).read(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
        System.out.println(data.length);
	}
	
//	 解压文件
//	 @param zipFile 目标文件
//	 @param descDir 指定解压目录
//	 @param urlList 存放解压后的文件目录（可选）
//	 @return
	 
	public static String  unZip(File zipFile, String descDir,String hzm) {
	    boolean flag = false;
	    String outPath="";
	    File pathFile = new File(descDir);
	    if(!pathFile.exists()){
	        pathFile.mkdirs();
	    }
	    ZipFile zip = null;
	    try {
	        //指定编码，否则压缩包里面不能有中文目录
	        zip = new ZipFile(zipFile);
	        for(Enumeration entries = zip.entries(); entries.hasMoreElements();){
	            ZipEntry entry = (ZipEntry)entries.nextElement();
	            String zipEntryName = entry.getName();
	            InputStream in = zip.getInputStream(entry);
	            outPath = (descDir+zipEntryName+hzm).replace("/", File.separator);
	            //判断路径是否存在,不存在则创建文件路径
	            File file = new File(outPath.substring(0, outPath.lastIndexOf(File.separator)));
	            if(!file.exists()){
	                file.mkdirs();
	            }
	            //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
	            if(new File(outPath).isDirectory()){
	                continue;
	            }
	           

	            OutputStream out = new FileOutputStream(outPath);
	            byte[] buf1 = new byte[2048];
	            int len;
	            while((len=in.read(buf1))>0){
	                out.write(buf1,0,len);
	            }
	            in.close();
	            out.close();
	        }
	        flag = true;
	        //必须关闭，否则无法删除该zip文件
	        zip.close();
	    } catch (IOException e) {
	    	outPath="文档解压错误";
	        e.printStackTrace();
	    }
	    return outPath;
	}
	public static String copyFile(String srcPathStr, String desPathStr,String hzm) {
        //1.获取源文件的名称
        String newFileName = srcPathStr.substring(srcPathStr.lastIndexOf("/")+1); //目标文件地址
        desPathStr = desPathStr + File.separator + newFileName+hzm; //源文件地址
        System.out.println("desPathStr:"+desPathStr);

        try{
            //2.创建输入输出流对象
            FileInputStream fis = new FileInputStream(srcPathStr);
            FileOutputStream fos = new FileOutputStream(desPathStr);                

            //创建搬运工具
            byte datas[] = new byte[1024*8];
            //创建长度
            int len = 0;
            //循环读取数据
            while((len = fis.read(datas))!=-1){
                fos.write(datas,0,len);
            }
            //3.释放资源
            fis.close();
            fis.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return desPathStr;
    }

}
